package com.jrh.web.admin;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.jrh.biz.img.ImgUploadService;
import com.jrh.dal.AtlasDao;
import com.jrh.dal.AtlasDetailDao;
import com.jrh.dal.BrandDao;
import com.jrh.dal.CategoryDao;
import com.jrh.dal.LabelDao;
import com.jrh.domain.entity.Atlas;
import com.jrh.domain.entity.AtlasDetail;
import com.jrh.domain.entity.Label;
import com.jrh.web.admin.form.AdminAtlasForm;
import com.jrh.web.admin.form.AdminFormCopier;

/**
 * 美图集后台管理
 * 
 * @author jasen.hsu.cn@gmail.com 2012-7-23 下午3:55:16
 */
@Controller
@RequestMapping("/admin/atlas")
public class AdminAtlasController {

    private static final Integer pageSize = 5;

    @Autowired
    private AtlasDao             atlasDao;

    @Autowired
    private LabelDao             labelDao;

    @Autowired
    private CategoryDao          categoryDao;

    @Autowired
    private AtlasDetailDao       atlasDetailDao;

    @Autowired
    private ImgUploadService     imgUploadService;

    @Autowired
    private BrandDao             brandDao;

    /**
     * 美图集后台首页
     * 
     * @param request
     * @return
     */
    @RequestMapping("")
    public String index(HttpServletRequest request, Integer currentPage, Model model) {
        model.addAttribute("_data_", atlasDao.list(null, atlasDao.getStartIndex(currentPage, pageSize), pageSize));
        model.addAttribute("styles", labelDao.listChildLabel(Label.LABEL_STYLE_KEY));
        model.addAttribute("categorys", categoryDao.listParentCategory());
        model.addAttribute("brands", brandDao.listAll());
        return "admin/atlas/list";
    }

    /**
     * 删除
     * 
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable Long id) {
        atlasDao.delete(id);
        return "redirect:/admin/atlas.htm";
    }

    @RequestMapping(method = RequestMethod.GET, value = "/create")
    public String create(Model model) {
        model.addAttribute("styles", labelDao.listChildLabel(Label.LABEL_STYLE_KEY));
        model.addAttribute("categorys", categoryDao.listParentCategory());
        return "admin/atlas/create";
    }

    @RequestMapping(method = RequestMethod.POST, value = "/create")
    public String doCreate(@ModelAttribute("atlasForm") AdminAtlasForm atlasForm) {

        String coverImgPath = imgUploadService.upload(atlasForm.getCoverImg(), "atlas");
        atlasForm.setCoverImgPath(coverImgPath);
        Atlas atlas = AdminFormCopier.convert(atlasForm, null);
        atlasDao.create(atlas);
        return "redirect:/admin/atlas.htm";
    }

    @RequestMapping(method = RequestMethod.GET, value = "/{id}")
    public String view(@PathVariable Long id, Model model) {
        Atlas atlas = atlasDao.load(id);
        List<AtlasDetail> atlasDetails = atlasDetailDao.listByAtlasId(id);
        // 美图
        model.addAttribute("atlas", atlas);
        // 详情
        model.addAttribute("atlasDetails", atlasDetails);
        return "admin/atlas/detail/list";
    }

    @RequestMapping(method = RequestMethod.GET, value = "/edit/{id}")
    public String edit(@PathVariable Long id, Model model) {
        Atlas atlas = atlasDao.load(id);
        model.addAttribute("_data_", atlas);
        model.addAttribute("styles", labelDao.listChildLabel(Label.LABEL_STYLE_KEY));
        model.addAttribute("categorys", categoryDao.listParentCategory());
        return "admin/atlas/edit";
    }

    @RequestMapping(method = RequestMethod.POST, value = "/edit")
    public String doEdit(@ModelAttribute("atlasForm") AdminAtlasForm atlasForm) {
        if (atlasForm != null && atlasForm.getCoverImg() != null && atlasForm.getCoverImg().getSize() > 0) {
            String coverImgPath = imgUploadService.upload(atlasForm.getCoverImg(), "atlas");
            atlasForm.setCoverImgPath(coverImgPath);
        }
        Atlas atlas = AdminFormCopier.convert(atlasForm, null);
        atlasDao.update(atlas);
        return "admin/success";
        // return "redirect:/admin/atlas.htm";
    }

    /**
     * 删除
     * 
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/delete/products/{id}")
    public String products(@PathVariable Long id) {

        return "redirect:/admin/atlas.htm";
    }

}
